import { createRouter, createWebHistory,createWebHashHistory } from "vue-router";

const routes = [
    {
        path: '/',
        children:[
            {
                path: '/',
                name: 'Home',
                meta: {title: '01星球博客--首页:欢迎各位程序员前来分享知识'},
                component: () => import('../views/Layout/Home/index.vue')
            },
            {
                path: '/article/:articleId',
                name: 'Aritcle',
                component: () => import('../views/Layout/Article/index.vue')
                
            },
            {
                path: '/articleedit',
                name: 'articleedit',
                meta: {title: '编辑文章'},
                component: () => import('../views/Layout/ArticleEdit/index.vue'),
            },
            {
                path: '/user/:type',
                name: 'gainaccess',
                meta: {title: '登录'},
                component: () => import('../views/Layout/Login/index.vue')
            },
            {
                path: '/search',
                name: 'search',
                meta: {title: '01星球博客--搜索'},
                component: () => import('../views/Layout/Search/index.vue')
            },
            {
                path: '/userinformation/:userid',
                name: 'userinformation',
                component: () => import('../views/Layout/UserInformation/index.vue')
            },
            {
                path: '/usercurrent',
                name: 'usercurrent',
                meta: {title: '个人中心'},
                component: () => import('../views/Layout/UserCurrent/index.vue')
            },
            {
                path: '/:page404',
                name: '404',
                meta: {title: '您搜索了一个不存在的页面'},
                component: () => import('../pages/404.vue')
            },
            {
                path: '/403error',
                name: '403',
                meta: {title: '您没有权限访问这个页面！'},
                component: () => import('../pages/403.vue')
            },
        ],
        component: () => import('../views/Layout/index.vue')
    },

]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to,from,next)=>{//beforeEach是router的钩子函数，在进入路由前执行
    if(to.meta.title){//判断是否有标题
        document.title = to.meta.title
    }
    next()  //执行进入路由，如果不写就不会进入目标页
})

export default router